package com.sunfeng.netty.heima.day03.part1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import static com.sunfeng.netty.heima.utils.NettyUtils.log;

/**
 * @ClassName EchoServer
 * @Description TODO
 * @Author 孙峰
 * @Date 2022/10/25 16:10
 */
@Slf4j
public class EchoServer {
    public static void main(String[] args) {
        new ServerBootstrap()
                .group(new NioEventLoopGroup(),new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                // 调整系统的接受缓冲区
               // .option(ChannelOption.SO_RCVBUF,10)
                // 调整netty的接受缓冲区
                //     .childOption(ChannelOption.RCVBUF_ALLOCATOR,new AdaptiveRecvByteBufAllocator(16,16,16))
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) {
                       ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) {
                                ByteBuf buffer = (ByteBuf) msg;

                                log(buffer);
//
//                                ByteBuf response = ctx.alloc().buffer();
//                                response.writeBytes(buffer);
//                                ctx.writeAndFlush(response);

                            }
                        });
                    }
                }).bind(8080);
    }
}
